package com.yh.util;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

    public class WifiSetupUtil {

        public static void setWifiAccountPassword(Context context, String ssid, String password) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiConfiguration wifiConfiguration = new WifiConfiguration();
            wifiConfiguration.SSID = "\"" + ssid + "\""; // 需要将SSID放在双引号中

            // 设置认证方式，有四种：WPA、WPA2、WEP、NOPASS
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

            // 设置加密方式，一般WPA/WPA2使用AES加密
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

            // 设置密码
            wifiConfiguration.preSharedKey = "\"" + password + "\""; // 需要将密码放在双引号中

            // 添加并激活网络
            int networkId = wifiManager.addNetwork(wifiConfiguration);
            wifiManager.disconnect();
            wifiManager.enableNetwork(networkId, true);
            boolean reCon = wifiManager.reconnect();
            System.out.println("WifiSetupUtil.setWifiAccountPassword = " +reCon);


        }
    }
